package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable String message){
        log.info("时间: {},发送给队列的消息:{}",new Date().toString(),message);
        rabbitTemplate.convertAndSend("XE","XQ1","发送给XQ1的消息:"+message);
        rabbitTemplate.convertAndSend("XE","XQ2","发送给XQ2的消息:"+message);
    }

    @GetMapping("/sendExpMsg/{message}/{titTime}")
    public void sendMsg(@PathVariable String message, @PathVariable String titTime){
        log.info("当前时间:{},发送一条时长{}毫秒TTL信息给队列Q3:{}",new Date(),titTime,message);
        rabbitTemplate.convertAndSend("XE","XQ3",message,msg-> {
            //发送消息的时候设置延迟时长
            msg.getMessageProperties().setExpiration(titTime);
            return msg;
        });
    }
}
